import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import '../ads/ad_feed_view.dart';
import '../ads/ads_const.dart';
import '../../utils/icon_unit.dart';
import '../ads/ads_utils.dart';

///通用提示弹出框
class DialogUtils {
  ///通用提示
  static Future<bool?>? showAlertDialog(
    BuildContext context, {
    String title = "温馨提示",
    String content = "提示内容",
    String cancelText = "取消",
    String confirmText = "确定",
    bool barrierDismissible = true,
    Function? onConfirmText,
    Function? onCancelText,
  }) async {
    List<Widget> actions = [];
    if (cancelText.isNotEmpty) {
      actions.add(TextButton(
        child: Text(
          cancelText,
          style: const TextStyle(
            fontSize: 13,
            color: Colors.grey,
          ),
        ),
        onPressed: () {
          Navigator.pop(context, false);
          onCancelText?.call();
        },
      ));
    }
    if (confirmText.isNotEmpty) {
      actions.add(
        TextButton(
          child: Text(
            confirmText,
            style: const TextStyle(
              fontSize: 13,
              color: Color(0xff1D2088),
            ),
          ),
          onPressed: () {
            Navigator.pop(context, true);
            onConfirmText?.call();
          },
        ),
      );
    }
    return showDialog(
      // 表示点击灰色背景的时候是否消失弹出框
      barrierDismissible: barrierDismissible,
      context: context,
      builder: (context) {
        return AlertDialog(
          title: title.isNotEmpty
              ? Center(
                  child: Text(
                    title,
                    style: const TextStyle(
                      fontSize: 14,
                      color: Colors.black,
                    ),
                  ),
                )
              : null,
          content: Text(
            content,
            style: const TextStyle(
              fontSize: 13,
              color: Colors.black,
            ),
          ),
          actions: actions,
        );
      },
    );
  }

  ///等待弹窗
  static Future showLoadingDialog(
    BuildContext context, {
    String content = "PleaseWait...",
    bool barrierDismissible = true,
  }) async {
    return await showDialog(
      // 表示点击灰色背景的时候是否消失弹出框
      barrierDismissible: barrierDismissible,
      context: context,
      barrierColor: Colors.transparent,
      builder: (context) {
        return Center(
          child: Container(
            width: 100,
            height: 100,
            decoration: const BoxDecoration(
              color: Colors.black54,
              borderRadius: BorderRadius.all(Radius.circular(6)),
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: [
                Center(
                  child: SizedBox(
                    height: 20,
                    width: 20,
                    child: CircularProgressIndicator(
                      strokeWidth: 2,
                      color: Colors.grey.shade300,
                    ),
                  ),
                ),
                const SizedBox(
                  height: 6,
                ),
                Material(
                  color: Colors.transparent,
                  child: Text(
                    "PleaseWait".tr,
                    style: TextStyle(
                      fontSize: 13,
                      color: Colors.grey.shade300,
                    ),
                  ),
                )
              ],
            ),
          ),
        );
      },
    );
  }

  ///等待弹窗
  static void showCompleteDialog(BuildContext context) {
    showDialog(
      // 表示点击灰色背景的时候是否消失弹出框
      barrierDismissible: true,
      context: context,
      barrierColor: Colors.transparent,
      builder: (context) {
        return FutureBuilder(
            future: Future.delayed(const Duration(milliseconds: 1000))
                .then((value) => true),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                Navigator.of(context).pop();
              }
              return Center(
                child: Container(
                  width: 160,
                  height: 160,
                  decoration: const BoxDecoration(
                    color: Colors.black54,
                    borderRadius: BorderRadius.all(Radius.circular(6)),
                  ),
                  child: Center(
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        const Icon(
                          Icons.check,
                          color: Colors.blueAccent,
                          size: 60,
                        ),
                        Material(
                          color: Colors.transparent,
                          child: Text(
                            "operateSuccessfully".tr,
                            style: const TextStyle(
                              color: Colors.white,
                              fontSize: 14,
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              );
            });
      },
    );
  }

  static Future<int?> showPhotoDialog(BuildContext context) async {
    return await showModalBottomSheet<int>(
      backgroundColor: Colors.transparent,
      context: context,
      builder: (context) {
        return Padding(
          padding: const EdgeInsets.all(12.0),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  MaterialButton(
                    materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                    minWidth: double.infinity,
                    height: 48,
                    color: Colors.white,
                    shape: const RoundedRectangleBorder(
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(6),
                        topRight: Radius.circular(6),
                        bottomLeft: Radius.circular(0),
                        bottomRight: Radius.circular(0),
                      ),
                    ),
                    onPressed: () {
                      Navigator.pop(context, 0);
                    },
                    child: const Text(
                      "相机",
                      style: TextStyle(
                        fontSize: 14,
                        color: Colors.black,
                      ),
                    ),
                  ),
                  MaterialButton(
                    materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                    minWidth: double.infinity,
                    height: 48,
                    color: Colors.white,
                    shape: const RoundedRectangleBorder(
                      borderRadius: BorderRadius.only(
                        bottomLeft: Radius.circular(6),
                        bottomRight: Radius.circular(6),
                        topLeft: Radius.circular(0),
                        topRight: Radius.circular(0),
                      ),
                    ),
                    onPressed: () {
                      Navigator.pop(context, 1);
                    },
                    child: const Text(
                      "相册",
                      style: TextStyle(
                        fontSize: 14,
                        color: Colors.black,
                      ),
                    ),
                  ),
                ],
              ),
              const SizedBox(
                height: 20,
              ),
              MaterialButton(
                materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                minWidth: double.infinity,
                height: 48,
                color: Colors.white,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(6),
                ),
                onPressed: () {
                  Navigator.pop(context, -1);
                },
                child: const Text(
                  "取消",
                  style: TextStyle(
                    fontSize: 14,
                    color: Colors.black38,
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  static Future showBack(
    BuildContext context, {
    String content = "PleaseWait...",
    bool barrierDismissible = true,
  }) async {
    return await showDialog(
      // 表示点击灰色背景的时候是否消失弹出框
      barrierDismissible: barrierDismissible,
      context: context,
      builder: (context) {
        return Center(
          child: Container(
            width: double.infinity,
            padding: const EdgeInsets.all(16.0),
            margin: const EdgeInsets.symmetric(horizontal: 16),
            decoration: const BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(6)),
            ),
            child: Material(
              color: Colors.transparent,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Row(
                    mainAxisSize: MainAxisSize.max,
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      InkWell(
                        onTap: () {
                          Navigator.pop(context);
                          AdsUtils.showInterstitialAd();
                        },
                        child: Container(
                          decoration: const BoxDecoration(
                            color: Colors.grey,
                            shape: BoxShape.circle,
                          ),
                          child: const Icon(
                            Icons.close,
                            color: Colors.white,
                          ),
                        ),
                      ),
                    ],
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  Material(
                    color: Colors.transparent,
                    child: Text(
                      "精彩内容不容错过，确定退出？".tr,
                      style: const TextStyle(
                        fontSize: 16,
                        color: Colors.grey,
                      ),
                    ),
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  Row(
                    mainAxisSize: MainAxisSize.max,
                    children: [
                      Expanded(
                        child: InkWell(
                          onTap: () async {
                            // await SystemChannels.platform
                            //     .invokeMethod('SystemNavigator.pop');
                            exit(0);
                          },
                          child: Container(
                            height: 44,
                            decoration: BoxDecoration(
                              color: Colors.grey.shade200,
                              borderRadius:
                                  const BorderRadius.all(Radius.circular(6)),
                            ),
                            child: Center(
                              child: Text(
                                "立即退出".tr,
                                style: const TextStyle(
                                  fontSize: 16,
                                  color: Colors.grey,
                                ),
                              ),
                            ),
                          ),
                        ),
                      ),
                      const SizedBox(
                        width: 16,
                      ),
                      Expanded(
                        child: InkWell(
                          onTap: () {
                            Navigator.pop(context);
                            AdsUtils.showInterstitialAd();
                          },
                          child: Container(
                            height: 44,
                            decoration: const BoxDecoration(
                              color: Colors.green,
                              borderRadius:
                                  BorderRadius.all(Radius.circular(6)),
                            ),
                            child: Center(
                              child: Text(
                                "再看看".tr,
                                style: const TextStyle(
                                  fontSize: 16,
                                  color: Colors.white,
                                ),
                              ),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                  const MyAdFeedView(),
                ],
              ),
            ),
          ),
        );
      },
    );
  }

  static Future<String?> showVipGet() async {
    return await Get.dialog(
      barrierDismissible: false,
      Scaffold(
        backgroundColor: Colors.transparent,
        body: PopScope(
          canPop: false,
          child: SingleChildScrollView(
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  SizedBox(
                    height: 50,
                    width: 260,
                    child: Row(
                      mainAxisSize: MainAxisSize.max,
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        InkWell(
                          onTap: () {
                            Get.back(result: "c");
                          },
                          child: Container(
                              decoration: const BoxDecoration(
                                color: Colors.grey,
                                shape: BoxShape.circle,
                              ),
                              child: const Icon(
                                Icons.close,
                                color: Colors.white,
                              )),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    height: 300,
                    width: double.infinity,
                    padding: const EdgeInsets.all(16.0),
                    margin: const EdgeInsets.symmetric(horizontal: 16),
                    decoration: const BoxDecoration(
                      //color: Colors.transparent,
                      //borderRadius:const BorderRadius.all(Radius.circular(6)),
                      image: DecorationImage(
                        image: AssetImage(
                          "assets/imagesff/bg_vip.png",
                        ),
                      ),
                    ),
                    child: Stack(
                      alignment: Alignment.bottomCenter,
                      children: [
                        Column(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Material(
                              color: Colors.transparent,
                              child: Text(
                                "永久会员限时发放中\n功能全免费纯净无广告".tr,
                                textAlign: TextAlign.center,
                                style: const TextStyle(
                                  fontSize: 16,
                                  color: Colors.grey,
                                ),
                              ),
                            ),
                            const SizedBox(
                              height: 16,
                            ),
                            InkWell(
                              onTap: () async {
                                //激励视频
                                Get.back(result: "j");
                              },
                              child: Container(
                                height: 44,
                                padding:
                                    const EdgeInsets.symmetric(horizontal: 16),
                                decoration: const BoxDecoration(
                                  color: Colors.blue,
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(44)),
                                ),
                                child: Row(
                                  mainAxisSize: MainAxisSize.min,
                                  children: [
                                    const Icon(
                                      Icons.play_circle_outline_sharp,
                                      color: Colors.white,
                                      size: 24,
                                    ),
                                    const SizedBox(
                                      height: 10,
                                    ),
                                    Text(
                                      "立即领取".tr,
                                      style: const TextStyle(
                                        fontSize: 16,
                                        color: Colors.white,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                            const SizedBox(
                              height: 16,
                            ),
                            InkWell(
                              onTap: () {
                                //放弃权益
                                //插屏
                                Get.back(result: "c");
                              },
                              child: Text(
                                "放弃权益".tr,
                                style: const TextStyle(
                                  fontSize: 16,
                                  color: Colors.grey,
                                ),
                              ),
                            ),
                            const SizedBox(
                              height: 16,
                            ),
                          ],
                        ),
                      ],
                    ),
                  ),
                  const MyAdFeedView(),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  // static Future showVip(
  //   BuildContext context, {
  //   String content = "PleaseWait...",
  //   bool barrierDismissible = true,
  // }) async {
  //   return await showDialog(
  //     // 表示点击灰色背景的时候是否消失弹出框
  //     barrierDismissible: barrierDismissible,
  //     context: context,
  //     builder: (context) {
  //       return Material(
  //         color: Colors.transparent,
  //         child: Center(
  //           child: Column(
  //             mainAxisAlignment: MainAxisAlignment.center,
  //             children: [
  //               SizedBox(
  //                 height: 50,
  //                 width: 260,
  //                 child: Row(
  //                   mainAxisSize: MainAxisSize.max,
  //                   mainAxisAlignment: MainAxisAlignment.end,
  //                   children: [
  //                     InkWell(
  //                       onTap: () {
  //                         Navigator.pop(context);
  //                         FlutterGroMoreAdsUtils.showInterstitialAd();
  //                       },
  //                       child: Container(
  //                           decoration: const BoxDecoration(
  //                             color: Colors.grey,
  //                             shape: BoxShape.circle,
  //                           ),
  //                           child: const Icon(
  //                             Icons.close,
  //                             color: Colors.white,
  //                           )),
  //                     ),
  //                   ],
  //                 ),
  //               ),
  //               Container(
  //                 height: 300,
  //                 width: double.infinity,
  //                 padding: const EdgeInsets.all(16.0),
  //                 margin: const EdgeInsets.symmetric(horizontal: 16),
  //                 decoration: BoxDecoration(
  //                   //color: Colors.transparent,
  //                   //borderRadius:const BorderRadius.all(Radius.circular(6)),
  //                   image: DecorationImage(
  //                     image: AssetImage(
  //                       IconUnit.localPicture("bg_vip"),
  //                     ),
  //                   ),
  //                 ),
  //                 child: Stack(
  //                   alignment: Alignment.bottomCenter,
  //                   children: [
  //                     Column(
  //                       mainAxisSize: MainAxisSize.min,
  //                       children: [
  //                         Material(
  //                           color: Colors.transparent,
  //                           child: Text(
  //                             "永久会员限时发放中\n功能全免费纯净无广告".tr,
  //                             textAlign: TextAlign.center,
  //                             style: const TextStyle(
  //                               fontSize: 16,
  //                               color: Colors.grey,
  //                             ),
  //                           ),
  //                         ),
  //                         const SizedBox(
  //                           height: 16,
  //                         ),
  //                         InkWell(
  //                           onTap: () {
  //                             Navigator.pop(context);
  //                             FlutterGroMoreAdsUtils.showRewardVideoAd();
  //                           },
  //                           child: Container(
  //                             height: 44,
  //                             padding:
  //                                 const EdgeInsets.symmetric(horizontal: 16),
  //                             decoration: const BoxDecoration(
  //                               color: Colors.blue,
  //                               borderRadius:
  //                                   BorderRadius.all(Radius.circular(44)),
  //                             ),
  //                             child: Row(
  //                               mainAxisSize: MainAxisSize.min,
  //                               children: [
  //                                 Image.asset(
  //                                   IconUnit.localPicture("ic_ad_1"),
  //                                   width: 24,
  //                                   height: 24,
  //                                 ),
  //                                 const SizedBox(
  //                                   height: 10,
  //                                 ),
  //                                 Text(
  //                                   "立即领取".tr,
  //                                   style: const TextStyle(
  //                                     fontSize: 16,
  //                                     color: Colors.white,
  //                                   ),
  //                                 ),
  //                               ],
  //                             ),
  //                           ),
  //                         ),
  //                         const SizedBox(
  //                           height: 16,
  //                         ),
  //                         InkWell(
  //                           onTap: () {
  //                             //放弃权益
  //                             Navigator.pop(context);
  //                             FlutterGroMoreAdsUtils.showInterstitialAd();
  //                           },
  //                           child: Text(
  //                             "放弃权益".tr,
  //                             style: const TextStyle(
  //                               fontSize: 16,
  //                               color: Colors.grey,
  //                             ),
  //                           ),
  //                         ),
  //                         const SizedBox(
  //                           height: 16,
  //                         ),
  //                       ],
  //                     ),
  //                   ],
  //                 ),
  //               ),
  //               //Expanded(child: FlutterGroMoreAdsUtils.getAdFeedWidget()),
  //             ],
  //           ),
  //         ),
  //       );
  //     },
  //   );
  // }

  static Future showPwd(
    BuildContext context, {
    String content = "PleaseWait...",
    bool barrierDismissible = true,
  }) async {
    String pwd = "";
    return await showDialog(
      // 表示点击灰色背景的时候是否消失弹出框
      barrierDismissible: barrierDismissible,
      context: context,
      builder: (context) {
        return Center(
          child: Container(
            width: double.infinity,
            padding: const EdgeInsets.all(16.0),
            margin: const EdgeInsets.symmetric(horizontal: 16),
            decoration: const BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(6)),
            ),
            child: Material(
              color: Colors.transparent,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                mainAxisSize: MainAxisSize.min,
                children: [
                  const SizedBox(
                    height: 10,
                  ),
                  TextField(
                    onChanged: (s) {
                      pwd = s;
                    },
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  Row(
                    mainAxisSize: MainAxisSize.max,
                    children: [
                      Expanded(
                        child: InkWell(
                          onTap: () async {
                            Navigator.pop(context);
                          },
                          child: Container(
                            height: 44,
                            decoration: BoxDecoration(
                              color: Colors.grey.shade200,
                              borderRadius:
                                  const BorderRadius.all(Radius.circular(6)),
                            ),
                            child: Center(
                              child: Text(
                                "取消".tr,
                                style: const TextStyle(
                                  fontSize: 16,
                                  color: Colors.grey,
                                ),
                              ),
                            ),
                          ),
                        ),
                      ),
                      const SizedBox(
                        width: 16,
                      ),
                      Expanded(
                        child: InkWell(
                          onTap: () {
                            Navigator.pop(context, pwd);
                          },
                          child: Container(
                            height: 44,
                            decoration: const BoxDecoration(
                              color: Colors.green,
                              borderRadius:
                                  BorderRadius.all(Radius.circular(6)),
                            ),
                            child: Center(
                              child: Text(
                                "确定".tr,
                                style: const TextStyle(
                                  fontSize: 16,
                                  color: Colors.white,
                                ),
                              ),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }
}
